一、力扣513.找树左下角的值 迭代 我的思路:层序遍历,寻找最后一层的第一个值,可以用一个集合记录每一层的第一个节点的数值,遍历结束后输出集合的最后一个值。 递归+回溯 1.确定递归函数的参数和返回值 参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。 本题还需要类里的两个全局变量,maxLen用来记录最大深度,result记录最大深度最左节点的数值。 2.确定终止条件 当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。 3.确定单层递归的逻辑 在找最大深度的时候,递归的过程中依然要使用回溯。 迭代 class Solution { public int findBottomLeftValue(TreeNode root) { Queue<TreeNode> que = new LinkedList<>(); List<Integer> res = new ArrayList<>(); if(root == null){ return root.val; }...
Is this you? As a journalist, you can create a free Muck Rack account to customize your profile, list your contact preferences, and upload a portfolio of your best work.
Claim your profile
Get in touch with int
Contact int, search articles and posts on X, monitor coverage, and track replies from one place.
Learn more about Muck Rack