Samsung Interview Question
Senior Software Development EngineersCountry: United States
Interview Type: Phone Interview
Traverse the Tree and store the node if there is no node in left->right or right->left
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> bottomView(TreeNode root) {
List<Integer> ret = new ArrayList<Integer>();
if ( root == null ) return ret;
if ( (root.left != null && root.left.right != null) == false || (root.right != null && root.left != null) == false )
ret.add(root.val);
List<Integer> left = bottomView(root.left);
ret.addAll(left);
List<Integer> right = bottomView(root.right);
ret.addAll(right);
return ret;
}
}
1 is the root node. Not able to get it in the right place in the question. Basically, 5 covers 1 in the bottom view.
- teli.vaibhav May 14, 2018