Leetcode中几道二叉树题 II
2014-05-25 19:44
239 查看
三、平衡二叉树 (Balanced Binary Tree)
题目一:Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced tree is defined as a binary tree in which the depth of the two subtree of every node never differ by more than 1.
思路:判断平衡二叉树是对树高度的判断,可以在对求树的高度算法上改造。一个树是平衡二叉树的充要条件是左右子树是平衡二叉树并且高度差不超过1.
题目二:Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
思路:求数的高度。经典算法。
题目三:Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思路:这题和上一题不一样,必须达到需要找到 leaf node 才行。
题目一:Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced tree is defined as a binary tree in which the depth of the two subtree of every node never differ by more than 1.
思路:判断平衡二叉树是对树高度的判断,可以在对求树的高度算法上改造。一个树是平衡二叉树的充要条件是左右子树是平衡二叉树并且高度差不超过1.
class Solution { public: vector<TreeNode*> generateTrees(int low, int high){ vector<TreeNode*> res; if(low>high){ res.push_back(NULL); //添加叶子节点也是十分必要的呀! return res; } else{ for(int i=low; i<=high; i++){ vector<TreeNode*> left=generateTrees(low, i-1); vector<TreeNode*> right=generateTrees(i+1, high); TreeNode *root; for(int j=0; j<left.size(); j++){ for(int k=0; k<right.size(); k++){ root=new TreeNode(i); root->left=left[j]; root->right=right[k]; res.push_back(root); } } } return res; } } vector<TreeNode *> generateTrees(int n) { vector<TreeNode*> res; if(n==0){ res.push_back(NULL); return res; } return generateTrees(1, n); } };
题目二:Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
思路:求数的高度。经典算法。
class Solution { public: int maxDepth(TreeNode *root) { if(root==NULL) return 0; //不一定是leaf node int left=maxDepth(root->left); int right=maxDepth(root->right); return 1+max(left, right); } };
题目三:Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
思路:这题和上一题不一样,必须达到需要找到 leaf node 才行。
class Solution { public: int minDepth(TreeNode *root) { if(root==NULL) return 0; if(root->left==NULL && root->right==NULL) return 1; //判定为叶子节点 int left,right; left=right=1<<12; if(root->left) left=minDepth(root->left); if(root->right) right=minDepth(root->right); return 1+min(left, right); } };
相关文章推荐
- [LeetCode] Binary Tree Longest Consecutive Sequence II 二叉树最长连续序列之二
- Leetcode 107. 二叉树的层次遍历 II
- Leetcode 95. Unique Binary Search Trees II及二叉树最大最小深度镜像树总结
- LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)
- leetcode_107题——Binary Tree Level Order Traversal II (二叉树,广度优先搜索,队列,栈)
- leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- LeetCode 95. Unique Binary Search Trees II&96. Unique Binary Search Trees--动态规划,二叉树
- leetcode 113. Path Sum II 二叉树求符合和值为sum的链
- LeetCode-95-Unique Binary Search Trees II 二叉树
- leetCode 113. Path Sum II 二叉树问题 | Medium
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- LeetCode | Binary Tree Level Order Traversal II(二叉树层序遍历II)
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
- [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II
- LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy
- leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)
- Binary Tree Level Order Traversal II 二叉树按层遍历(反向输出)@LeetCode
- Leetcode中几道二叉树题 (IV)
- [LeetCode] Path Sum II 二叉树路径之和之二