515.Find Largest Value in Each Tree Row
发布时间:2020-12-14 04:15:38 所属栏目:大数据 来源:网络整理
导读:You need to find the largest value in each row of a binary tree.Example:?Input: 1 / 3 2 / 5 3 9 Output: [ 1,3,9 ]Bfs public int [] findValueMostElement(TreeNode root) { Queue TreeNode queue = new LinkedListTreeNode (); List Integer res
You need to find the largest value in each row of a binary tree. Example:? Input: 1 / 3 2 / 5 3 9 Output: [1,3,9] Bfs public int[] findValueMostElement(TreeNode root) { Queue<TreeNode> queue = new LinkedList<TreeNode>(); List<Integer> res = new ArrayList<Integer>(); queue.add(root); int queueSize = root == null ? 0 : 1; while (queueSize > 0) { int largestElement = Integer.MIN_VALUE; for (int i=0;i<queueSize;i++) { TreeNode cur = queue.poll(); largestElement = Math.max(cur.val,largestElement); if (cur.left != null) queue.add(cur.left); if (cur.right != null) queue.add(cur.right); } res.add(largestElement); queueSize = queue.size(); } int[] resArray = new int[res.size()]; for (int i=0;i<res.size();i++) resArray[i] = res.get(i); return resArray; } Dfs Just a simple pre-order traverse idea. Use depth to expand result list size and put the max value in the appropriate position. public class Solution { public List<Integer> largestValues(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); helper(root,res,0); return res; } private void helper(TreeNode root,List<Integer> res,int d){ if(root == null){ return; } //expand list size if(d == res.size()){ res.add(root.val); } else{ //or set value res.set(d,Math.max(res.get(d),root.val)); } helper(root.left,d+1); helper(root.right,d+1); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |