513. 找树左下角的值
- 原题链接:
- 完成情况:
- 解题思路:
- 参考代码:
- __513找树左下角的值__BFS
- __513找树左下角的值__从右往左出队
- 错误经验吸取
原题链接:
513. 找树左下角的值
https://leetcode.cn/problems/find-bottom-left-tree-value/description/
完成情况:
解题思路:
看代码里的注释
参考代码:
__513找树左下角的值__BFS
package 西湖算法题解___中等题;import java.util.LinkedList;
import java.util.Queue;public class __513找树左下角的值__BFS {public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}int maxHight = 0; //需要记录获取res时,你对应的heightint res = 0;public int findBottomLeftValue(TreeNode root) {//层次遍历肯定行不通啦= =~
/* //最底层的最左边的元素值Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while ()*//**考虑使用深度优先搜索的话,应该会可行。即,每次bfs,遍历到的第一个就是最终的最深处的最左边的那一个但是同时你需要记录到【当前的高度】。*/dfs__findBottomLeftValue(root,0);return res;}/**** @param root 当前位置结点值* @param curHight 当前高度*/private void dfs__findBottomLeftValue(TreeNode root, int curHight) {if (root == null){return;}//那么就要去遍历子节点curHight++;dfs__findBottomLeftValue(root.left,curHight);dfs__findBottomLeftValue(root.right,curHight);//左右都走不动了,我们再来判断当前位置结点。if (curHight > maxHight){maxHight = curHight;res = root.val;}}
}
__513找树左下角的值__从右往左出队
package 西湖算法题解___中等题;import java.util.ArrayDeque;
import java.util.Queue;public class __513找树左下角的值__从右往左出队 {public class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}/*用队列,层次排序,从右往左遍历,给每一个值都赋res,最终得到res的一定是最底层且最左边的*/public int findBottomLeftValue(TreeNode root) {int res = 0;Queue<TreeNode> queue = new ArrayDeque<TreeNode>();queue.offer(root);while (!queue.isEmpty()){TreeNode curNode = queue.poll();if (curNode.right != null){queue.offer(curNode.right);}if (curNode.left != null){queue.offer(curNode.left);}res = curNode.val;}return res;}
}