力扣算法刷题 Day 14
226. 翻转二叉树题目链接226. 翻转二叉树 - 力扣LeetCode思路递归法交换左右子树即可函数退出状态为当前节点为NULL。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root NULL) return root; swap(root-left,root-right); invertTree(root-left); invertTree(root-right); return root; } };迭代法之前写过迭代法遍历自然也可以写出这个。注意用到栈的数据结构左右子树入栈时先右边入栈。以及操作时避免空指针异常需要先做判断。TreeNode* invertTree(TreeNode* root) { stack TreeNode* st; if(root NULL) return root; st.push(root); while(!st.empty()) { TreeNode* tmp st.top(); st.pop(); swap(tmp-left,tmp-right); if(tmp-right) st.push(tmp-right); if(tmp-left) st.push(tmp-left); } return root; }文章详解226.翻转二叉树 | 代码随想录101. 对称二叉树题目链接101. 对称二叉树 - 力扣LeetCode思路递归法要判断是否反转就要比较左边的外/内侧节点和右边的外/内侧节点是否相同。函数的返回值应该是bool类型入口参数就是左右孩子节点。递归结束条件为左右孩子存在空或都不空且数值不相等。进入递归逻辑左树要 左右根右树要右左根。文章详解101. 对称二叉树 | 代码随想录/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: bool compare(TreeNode *left,TreeNode * right) { if(left NULL right ! NULL) return false; if(right NULL left ! NULL) return false; if(left NULL right NULL) return true; if(left-val ! right-val) return false; bool out compare(left-left,right-right); bool in compare(left-right,right-left); return outin; }; bool isSymmetric(TreeNode* root) { if(root NULL) return true; return compare(root-left,root-right); } };104. 二叉树的最大深度题目链接104. 二叉树的最大深度 - 力扣LeetCode思路递归法函数返回值int 入口参数根节点root 结束条件当前节点为空 递归求左/右子树的深度取最大值加1/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int getdepth(TreeNode * root) { if(root NULL) return 0; int ld getdepth(root-left); int rd getdepth(root-right); int max1 1 max(ld,rd); return max1; } int maxDepth(TreeNode* root) { int dep getdepth(root); return dep; } };迭代法按照层序遍历的思路遍历多少层就是深度多少。注意数据结构的选择为队列。int maxDepth(TreeNode* root) { queue TreeNode * que; if(root NULL) return 0; que.push(root); int depth 0; while(!que.empty()) { int size que.size(); depth; for(int i 0; i size; i) { TreeNode * node que.front(); que.pop(); if(node-left) que.push(node-left); if(node-right) que.push(node-right); } } return depth; }文章详解104.二叉树的最大深度 | 代码随想录111. 二叉树的最小深度题目链接111. 二叉树的最小深度 - 力扣LeetCode思路和最大深度的差异在于最小深度的定义为 叶子节点到根节点的最小距离。何为叶子节点左右子节点均为空。因此当有一个子节点为空而另一个不为空时该节点不为最短节点。文章详解111.二叉树的最小深度 | 代码随想录/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int getdepth(TreeNode* root) { if(root NULL) return 0; int ld getdepth(root-left); int rd getdepth(root-right); if(root-left NULL root-right ! NULL) { return 1 rd; } else if(root-right NULLroot-left ! NULL ) { return 1 ld; } int minidep min(ld,rd) 1; return minidep; } int minDepth(TreeNode* root) { return getdepth(root); } };