国产探花免费观看_亚洲丰满少妇自慰呻吟_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,就不會再執行右子樹的了    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 苍溪县| 房产| 平昌县| 太和县| 英吉沙县| 江津市| 贡嘎县| 双牌县| 南昌县| 通渭县| 金平| 勃利县| 横峰县| 广德县| 简阳市| 辛集市| 台东县| 屏山县| 南京市| 南投县| 莱阳市| 仪征市| 娱乐| 沽源县| 颍上县| 海门市| 通化县| 金寨县| 九台市| 镇江市| 肥乡县| 陵水| 循化| 子长县| 永清县| 南京市| 弥渡县| 元江| 宁强县| 越西县| 红桥区|