545. Boundary of Binary Tree

545. Boundary of Binary Tree

Still wrong result 

class Solution {
    public List<Integer> boundaryOfBinaryTree(TreeNode root) {
      List<Integer> result = new ArrayList<>();
      if(root == null){
        return result;
      }
      
      if(root.left == null || root.right == null) result.add(root.val);
      leftbound(root.left,result);
      leaves(root,result);
      rightbound(root,result);
      result.remove(result.size() - 1);
      return result;  
    }
    private void leftbound(TreeNode root,List<Integer> result){
      if(root == null || (root.left == null && root.right == null)){
        return;
      }
      result.add(root.val);
      if(root.left == null){
        leftbound(root.right,result);
      }else{
        leftbound(root.left,result);
      }
    }
    
    private void rightbound(TreeNode root,List<Integer> result){
      Deque<Integer> stack = new LinkedList<>();
      if(root == null || (root.left == null  && root.right == null)){
        return;
      }
      stack.push(root.val);
      if(root.right == null){
        rightbound(root.left,result);
      }else{
        rightbound(root.right,result);
      }
      int size = stack.size();
      for(int i = 0; i < size; i++){
          int cur = stack.pop();
          result.add(cur);
      }
    }
    
    private void leaves(TreeNode root,List<Integer> result){
      if(root == null) return;
      if(root.left == null && root.right == null) result.add(root.val);
      leaves(root.left,result);
      leaves(root.right,result);
    }
}

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...