原题链接在这里:https://leetcode.com/problems/boundary-of-binary-tree/description/
题目:
Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes.
Left boundary is defined as the path from root to the left-most node. Right boundary is defined as the path from root to the right-most node. If the root doesn't have left subtree or right subtree, then the root itself is left boundary or right boundary. Note this definition only applies to the input binary tree, and not applies to any subtrees.
The left-most node is defined as a leaf node you could reach when you always firstly travel to the left subtree if exists. If not, travel to the right subtree. Repeat until you reach a leaf node.
The right-most node is also defined by the same way with left and right exchanged.
Example 1
Input: 1 \ 2 / \ 3 4 Ouput: [1, 3, 4, 2] Explanation: The root doesn't have left subtree, so the root itself is left boundary. The leaves are node 3 and 4. The right boundary are node 1,2,4. Note the anti-clockwise direction means you should output reversed right boundary. So order them in anti-clockwise without duplicates and we have [1,3,4,2].
Example 2
Input: ____1_____ / \ 2 3 / \ / 4 5 6 / \ / \ 7 8 9 10 Ouput: [1,2,4,7,8,9,10,6,3] Explanation: The left boundary are node 1,2,4. (4 is the left-most node according to definition) The leaves are node 4,7,8,9,10. The right boundary are node 1,3,6,10. (10 is the right-most node). So order them in anti-clockwise without duplicate nodes we have [1,2,4,7,8,9,10,6,3].
题解:
原问题可以分解成三块,加左边界,从左向右加leaf节点,加右边界.
找左边界,在当前节点不为叶子节点时把它的值加到res中,然后像左边继续移动,若左边是null, 就像右边移动.
加右边界就是反过来.
加叶子节点, 判断当前点是不是叶子, 是就加入, 不是就先找left child, 再找 right child.
Time Complexity: O(n). 找左右边界用时O(logn). 找叶子用时O(n).
Space: O(logn), stack space, regardless res.
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 class Solution { 11 public List<Integer> boundaryOfBinaryTree(TreeNode root) { 12 List<Integer> res = new ArrayList<Integer>(); 13 if(root == null){ 14 return res; 15 } 16 17 if(!isLeaf(root)){ 18 res.add(root.val); 19 } 20 21 // add left boundary 22 TreeNode l = root.left; 23 while(l != null){ 24 if(!isLeaf(l)){ 25 res.add(l.val); 26 } 27 28 if(l.left != null){ 29 l = l.left; 30 }else{ 31 l = l.right; 32 } 33 } 34 35 // add leaves 36 addLeaves(root, res); 37 38 // add right boundary 39 TreeNode r = root.right; 40 Stack<Integer> stk = new Stack<Integer>(); 41 while(r != null){ 42 if(!isLeaf(r)){ 43 stk.push(r.val); 44 } 45 46 if(r.right != null){ 47 r = r.right; 48 }else{ 49 r = r.left; 50 } 51 } 52 53 while(!stk.isEmpty()){ 54 res.add(stk.pop()); 55 } 56 57 return res; 58 } 59 60 private boolean isLeaf(TreeNode root){ 61 return root.left == null && root.right == null; 62 } 63 64 private void addLeaves(TreeNode root, List<Integer> res){ 65 if(root == null){ 66 return; 67 } 68 69 if(isLeaf(root)){ 70 res.add(root.val); 71 }else{ 72 addLeaves(root.left, res); 73 addLeaves(root.right, res); 74 } 75 } 76 }