leetcode 513. Find Bottom Left Tree Value(C语言,二叉树,深度,递归)54
来源:互联网 发布:c语言课程设计题目汇总 编辑:程序博客网 时间:2024/06/17 20:10
贴原题:
解析:
本题是给出一个二叉树,让求其最左端的值。
所谓的最左端其实就是最深层的那一行,最最边的那个数。那么我们只需要先求出二叉树的深度,然后再找出深度等于二叉数深度的那一行最左侧节点的值就可以了。
贴代码:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */int treeDeep(struct TreeNode* root)//递归计算二叉树的深度{ if(!root) { return 0; } int l_deep=0, r_deep=0; l_deep=1+treeDeep(root->left); r_deep=1+treeDeep(root->right); return l_deep>r_deep?l_deep:r_deep;}int leftmost(struct TreeNode* root, int deep, int now_deep)//递归寻找与二叉树深度相同且在最左侧的值{ if(!root) { return ' ';//空节点返回空 } if(now_deep==deep && root)//二叉树深度相同且存在的值 { return root->val; } int l=leftmost(root->left, deep, 1+now_deep); int r=leftmost(root->right, deep, 1+now_deep); if(l!=' ')//优先返回左侧值 { return l; } return r;}int findBottomLeftValue(struct TreeNode* root) { int deep=treeDeep(root);//计算树的深度 return leftmost(root, deep, 1);//返回最左侧节点}
阅读全文
0 0
- leetcode 513. Find Bottom Left Tree Value(C语言,二叉树,深度,递归)54
- 【leetcode】513. Find Bottom Left Tree Value(Python & C++)
- LeetCode 513. Find Bottom Left Tree Value (C++)
- LeetCode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 【LeetCode】513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- leetcode-513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- mac上c#环境搭建
- ARM-A8嵌入式综合实训开发记录-YUY2(YUYV)转RGB
- 164. Maximum Gap
- Linux下C语言高级编程之gcc高级用法?
- ng generate component Error More than one module matches. Use skip-import opti
- leetcode 513. Find Bottom Left Tree Value(C语言,二叉树,深度,递归)54
- pid_t的类型定义
- Linux 高速下载器XDM
- windows hadoop2.7.0.zip
- Spark SQL
- 文件上传
- Spring Aop(十六)——编程式的自定义Advisor
- Makefile编写
- No.17【大数据算法】Bloom Filter 的数学背景