国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[leetcode]513. Find Bottom Left Tree Value

2019-11-08 02:38:58
字體:
來源:轉載
供稿:網友

Given a binary tree, find the leftmost value in the last row of the tree.

Example 1:

Input:    2   / /  1   3Output:1

Example 2:

Input:        1       / /      2   3     /   / /    4   5   6       /      7Output:7

Note:You may assume the tree (i.e., the given root node) is notNULL.

思路:找二叉樹最底層最左邊的值,使用層次遍歷

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    PRivate int result = 0;    private int height = 0;        public int findBottomLeftValue(TreeNode root) {        search(root, 1);        return result;    }        private void search(TreeNode root, int depth) {        if(root==null) return;                if(height<depth) {            result = root.val;            height = depth;        }                search(root.left, depth+1);        search(root.right, depth+1); //如果同一左右子樹都有,執行完左子樹后height=depth,就不會再執行右子樹的了    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 左云县| 泸州市| 乌兰县| 改则县| 兴山县| 闵行区| 安乡县| 左权县| 隆化县| 聂拉木县| 梅州市| 白城市| 台中县| 麟游县| 安福县| 哈巴河县| 昌邑市| 桑日县| 泰安市| 白河县| 兴海县| 北辰区| 鲜城| 涿州市| 克拉玛依市| 盘锦市| 屏东市| 甘谷县| 开阳县| 平舆县| 本溪市| 大方县| 桂东县| 丹凤县| 溧水县| 上栗县| 城口县| 佛坪县| 广饶县| 临漳县| 攀枝花市|