摘要: 要求 给出一个二叉树及数字sum,判断是否存在一条从根到叶子的路径,路径上的所有节点和为sum 实现 转化为寻找左右子树上和为 sum-root 的路径,到达叶子节点时递归终止 注意只有一个孩子时,根节点本身不构成一条路径,如下图sum=5的情况,终止条件是不对的 1 class Solution 阅读全文
posted @ 2020-04-11 11:43 cxc1357 阅读(115) 评论(0) 推荐(0)
摘要: 要求 翻转一棵二叉树 实现 翻转左右子树,交换左右子树的根节点 1 class Solution { 2 public: 3 TreeNode* invertTree(TreeNode* root) { 4 5 if( root == NULL ) 6 return NULL; 7 8 invert 阅读全文
posted @ 2020-04-11 11:08 cxc1357 阅读(113) 评论(0) 推荐(0)
摘要: 要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int val; 4 TreeNode *left; 5 TreeNode *right; 6 TreeNode 阅读全文
posted @ 2020-04-11 10:48 cxc1357 阅读(84) 评论(0) 推荐(0)