加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

545.Boundary of Binary Tree

发布时间:2020-12-14 03:50:44 所属栏目:大数据 来源:网络整理
导读:545 .?Boundary of Binary TreeStill wrong result class Solution { public ListInteger boundaryOfBinaryTree(TreeNode root) { List Integer result = new ArrayList (); if (root == null ){ return result; } if (root.left == null || root.right == n
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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读