leetcode513. 找树左下角的值(dfs)
生活随笔
收集整理的這篇文章主要介紹了
leetcode513. 找树左下角的值(dfs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個二叉樹,在樹的最后一行找到最左邊的值。
代碼
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/ class Solution {int maxL=0,maxN=0;public int findBottomLeftValue(TreeNode root) {BottomLeft(root,1);return maxN;}public void BottomLeft(TreeNode root,int level) {//先序遍歷if(root==null) return;if(maxL<level)//如果找出更深的節點,替換掉{maxL=level;maxN=root.val;}BottomLeft(root.left,level+1);BottomLeft(root.right,level+1);} }總結
以上是生活随笔為你收集整理的leetcode513. 找树左下角的值(dfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到鸭是什么意思周公解梦
- 下一篇: 同一晚好几个人梦到自己