LeetCode199 打印树最右边的值

原创 2016年05月31日 14:50:56

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example:
Given the following binary tree,

   1            <---
 /   \
2     3         <---
 \     \
  5     4       <---

You should return [1, 3, 4].


由于刚实现了分层打印二叉树的功能,并且可以记录行号。那么实现上面这个功能就不难了。在分层打印二叉树的基础上,判断cur,last节点是否重合,重合就是最后一个元素了,打印出来就行了。


/**
 * Created by kyle on 2016/5/31.
 */
public class RIghtSideTree {

  static   public List<Integer> rightSideView(TreeNode root) {
        if(root == null){
            return  new ArrayList<Integer>();
        }
        TreeNode currentTreeNode = new TreeNode(0);
        List<Integer> list = new ArrayList<Integer>();
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        int cur =0, last =1;
        while(queue.size() != 0){
            while(cur < last){
                currentTreeNode = queue.remove();
                if(currentTreeNode.left != null){
                    queue.add(currentTreeNode.left);
                }
                if(currentTreeNode.right != null){
                    queue.add(currentTreeNode.right);
                }
                cur++;
            }
            list.add(currentTreeNode.val);

            cur =0;
            last = queue.size();
        }
        return  list;
    }

    static  TreeNode insertTree(TreeNode root, int val){
        if(root == null){
            root = new TreeNode(val);
            return root;
        }

        if(root.val >val){
            if(root.left == null){
                root.left = new TreeNode(val);
            }else {
                insertTree(root.left, val);
            }
        }else{
                if(root.right == null){
                    root.right = new TreeNode(val);
                }else {
                    insertTree(root.right, val);
                }

        }
        return  root;
    }

   static void showTree(TreeNode root){
        if(root == null){
           // System.out.println("[]");
            return;
        }

        showTree(root.left);
       System.out.println(root.val);
        showTree(root.right);
    }
    static  public  void main(String[] args){
        TreeNode tr;
        tr = null;
        tr = insertTree(tr, 18);
        tr = insertTree(tr, 5);
        tr = insertTree(tr, 2);
        tr = insertTree(tr, 8);
        tr = insertTree(tr, 81);
        tr = insertTree(tr, 101);
        System.out.println("Original:\n");
        showTree(tr);
       List<Integer> list = rightSideView(tr);
        System.out.println("side way");
        System.out.println(list);
    }
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】

【199-Binary Tree Right Side View(从右边看二叉树】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github.com/W...

leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python

原题链接:199. Binary Tree Right Side View 【思路】非递归实现 层次遍历,每一层都是从左到右遍历,将每一层的最右一个节点添加到结果集中就是我们需要得到的结果: publ...

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

LeetCode-199. Binary Tree Right Side View (JAVA)(二叉树最右侧结点)

Leetcode 199 Binary Tree Right Side View 二叉树右视图

给出一个二叉树,设想你从树的右侧看过来,返回你从上往下可以看到的结点....

【Leetcode】从右边看二叉树

原题出自LeetCode: Given a binary tree, imagine yourself standing on the right side of it, return the val...

[JAVA]LeetCode199 Binary Tree Right Side View

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod...
  • fumier
  • fumier
  • 2015年04月09日 19:41
  • 702

LeetCode题解-199-Binary Tree Right Side View

原题 原题链接:https://leetcode.com/problems/binary-tree-right-side-view/ 解法概览 解法1:迭代法,BFS 解法2:...

[LeetCode]199.Binary Tree Right Side View

题目Given a binary tree, imagine yourself standing on the right side of it, return the values of the n...

[Leetcode] 199. Binary Tree Right Side View 解题报告

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values o...

[LeetCode 199] Binary Tree Right Side View

题目链接:binary-tree-right-side-view import java.util.ArrayList; import java.util.List; /** * ...
  • ever223
  • ever223
  • 2015年04月15日 17:23
  • 2396
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode199 打印树最右边的值
举报原因:
原因补充:

(最多只允许输入30个字)