110.平衡二叉树
代码随想录链接
题目
判断一个二叉树是否是平衡二叉树,
即该二叉树的任意一个子树的左右子树高度差都不超过1.
自己的想法
递归判断左右子树的高度是否满足要求.
题解
递归法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { private: int getHeight(TreeNode* node) { if (!node) return 0; int leftHeight = getHeight(node -> left); if (leftHeight == -1) return -1; int rightHeight = getHeight(node -> right); if (rightHeight == -1) return -1; return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight); } public: bool isBalanced(TreeNode* root) { return getHeight(root) == -1 ? false : true; } };
|
迭代法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Solution { private: int getDepth(TreeNode* cur) { if (!cur) return 0; int depth = 0; queue<TreeNode *> que; que.push(cur); while (!que.empty()) { int qsize = que.size(); depth++; for (int i = 0; i < qsize; i++) { TreeNode *node = que.front(); que.pop(); if (node -> left) que.push(node -> left); if (node -> right) que.push(node -> right); } } return depth; } public: bool isBalanced(TreeNode* root) { if (!root) return true; stack<TreeNode *> st; st.push(root); while (!st.empty()) { TreeNode *node = st.top(); st.pop(); if (abs(getDepth(node -> left) - getDepth(node -> right)) > 1) { return false; } if (node -> right) st.push(node -> right); if (node -> left) st.push(node -> left); } return true; } };
|
这个地方的迭代法重复计算了好些层的数值,效率没有上面递归法高。
257.二叉树的所有路径
代码随想录链接
题目
返回从根节点出发到达每个叶子节点的路径。
自己的想法
前序遍历,访问一个节点的操作是判断其是否为叶子节点,若为叶子节点,则从根节点到这个节点的路径记录下来。
题解
递归法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| class Solution { private: void traversal(TreeNode *cur, vector<int> &path, vector<string> &result) { path.push_back(cur -> val); if (!(cur -> left) && !(cur -> right)) { string sPath; for (int i = 0; i < path.size() - 1; i++) { sPath += to_string(path[i]); sPath += "->"; } sPath += to_string(cur -> val); result.push_back(sPath); } if (cur -> left) { traversal(cur -> left, path, result); path.pop_back(); } if (cur -> right) { traversal(cur -> right, path, result); path.pop_back(); } } public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; vector<int> path; if (!root) return result; traversal(root, path, result); return result; } };
|
迭代法:
前序遍历,并使用栈来记录当前遍历从根节点出发的路径。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| class Solution { public: vector<string> binaryTreePaths(TreeNode* root) { vector<string> result; stack<string> pathSt; stack<TreeNode*> st; if (!root) return result; st.push(root); pathSt.push(to_string(root -> val)); while (!st.empty()) { TreeNode *node = st.top(); st.pop(); string path = pathSt.top(); pathSt.pop(); if (!(node -> left) && !(node -> right)) { result.push_back(path); } if (node -> right) { st.push(node -> right); pathSt.push(path + "->" + to_string(node -> right -> val)); } if (node -> left) { st.push(node -> left); pathSt.push(path + "->" + to_string(node -> left -> val)); }
} return result; } };
|
404.左叶子之和
代码随想录链接
题目
返回给定的二叉树的所有左叶子结点的值之和.
自己的想法
在遍历的时候加一个判断, 如果左孩子存在且是叶子结点,
就将左孩子的值加和.
题解
递归法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public: int sumOfLeftLeaves(TreeNode* root) { if (!root) return 0; if (!(root -> left) && !(root -> right)) return 0; int leftVal = sumOfLeftLeaves(root -> left); if (root -> left && !(root -> left -> left) && !(root -> left -> right)) { leftVal += root -> left -> val; } int rightVal = sumOfLeftLeaves(root -> right);
return leftVal + rightVal; } };
|
迭代法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: int sumOfLeftLeaves(TreeNode* root) { int result = 0; if (!root) return result; stack<TreeNode *> st; st.push(root); while (!st.empty()) { TreeNode *node = st.top(); if (node) { st.pop(); st.push(node); st.push(NULL);
if (node -> right) st.push(node -> right); if (node -> left) st.push(node -> left); } else { st.pop(); node = st.top(); st.pop(); if (node -> left && !(node -> left -> left) && !(node -> left -> right)) result += node -> left -> val; } } return result; } };
|