Leetcode--easy系列6
来源:互联网 发布:数据分析兼职平台 编辑:程序博客网 时间:2024/05/18 02:37
#104 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.
求二叉树的深度。二叉树的操作许多都可以用递归
//7ms/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) { int l_depth,r_depth; if(!root) return 0; else { l_depth = maxDepth(root->left); r_depth = maxDepth(root->right); return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1); } }
#110 Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
判断给定二叉树是否是平衡二叉树。。。深度差小于等于1.可以递归求每个结点的深度,判断其子树高度差。
//8ms/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int maxDepth(struct TreeNode* root) { int l_depth,r_depth; if(!root) return 0; else { l_depth = maxDepth(root->left); r_depth = maxDepth(root->right); return (l_depth >= r_depth) ? (l_depth+1):(r_depth+1); } }bool isBalanced(struct TreeNode* root) { int k; if(!root)//空树 return true; k=maxDepth(root->left)-maxDepth(root->right); if(abs(k)>1) return false; else return isBalanced(root->left) && isBalanced(root->right);}
#111 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.
求二叉树的最小深度---//4ms/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int minDepth(struct TreeNode* root) { int l_depth,r_depth; if(!root) return 0; if(!root->left) return 1+minDepth(root->right); if(!root->right) return 1+minDepth(root->left); if(root->right && root->left) { l_depth = minDepth(root->left); r_depth = minDepth(root->right); return (l_depth < r_depth) ? (l_depth+1) : (r_depth+1); } }#112 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:Given the below binary tree and
sum = 22
,5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
判定指定二叉树是否存在路径和等于指定值。
//8ms/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */bool hasPathSum(struct TreeNode* root, int sum) { if(!root) return false; if( root->left==NULL && root->right==NULL) return sum == root->val; else return hasPathSum(root->left,sum - root->val) || hasPathSum(root->right,sum - root->val); }
- Leetcode--easy系列6
- Leetcode--easy系列1
- Leetcode--easy系列2
- Leetcode--easy系列3
- Leetcode--easy系列4
- Leetcode--easy系列5
- Leetcode--easy系列7
- Leetcode--easy系列8
- Leetcode--easy系列9
- Leetcode--easy系列10
- Leetcode(6) - easy
- LeetCode系列Easy【1】 Algorithms - Bit manipulation
- LeetCode系列Easy【2】 Algorithms - Array
- LeetCode修炼之路(Easy系列)
- Leetcode修炼之路(Easy系列)
- Leetcode修炼之路(Easy系列) --ReverseInteger
- [Leetcode 6, Easy] Zigzag Conversion
- <LeetCode><Easy> 6 ZigZag Conversion
- ReactJS 傻瓜教程
- SQL 触发器
- 已知一个数组 20 个元素(随机 1 到 100 之间包含 1 和 100),求大于平均数的元素个数,并动态生成一个新数组保存
- LeetCode:Excel Sheet Column Number
- win32 启动和结束进程
- Leetcode--easy系列6
- XAMPP本地访问慢
- Objective-C类的定义详解与实例
- iOS-KVO究竟怎么用
- Android---59---Toast的使用
- HLJU周赛5解题报告
- 版本控制工具总结
- iOS学习笔记(3)UIButton
- Sicily 1934. 移动小球