LeetCode Print Binary Tree

来源:互联网 发布:网络十大神豪 编辑:程序博客网 时间:2024/05/17 07:54

Print a binary tree in an m*n 2D string array following these rules: 

  1. The row number m should be equal to the height of the given binary tree.
  2. The column number n should always be an odd number.
  3. The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them. 
  4. Each unused space should contain an empty string "".
  5. Print the subtrees following the same rules.

Example 1:

Input:     1    /   2Output:[["", "1", ""], ["2", "", ""]]

Example 2:

Input:     1    / \   2   3    \     4Output:[["", "", "", "1", "", "", ""], ["", "2", "", "", "", "3", ""], ["", "", "4", "", "", "", ""]]

Example 3:

Input:      1     / \    2   5   /   3  / 4 Output:[["",  "",  "", "",  "", "", "", "1", "",  "",  "",  "",  "", "", ""] ["",  "",  "", "2", "", "", "", "",  "",  "",  "",  "5", "", "", ""] ["",  "3", "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""] ["4", "",  "", "",  "", "", "", "",  "",  "",  "",  "",  "", "", ""]]

Note: The height of binary tree is in the range of [1, 10].

题解:此题在给定一棵二叉树后,按照一定的规则将每一行的结果输出。如果遇到不存在的节点,则将这个位置的输出置为“”。

此题我一开始的思路不是特别好,考虑用层次遍历,后来看到一篇博客的文章,http://blog.csdn.net/weixin_39071610/article/details/77018592,发现他采用了非常巧妙的递归的方式,后来一经分析,发现的确可以用,其实每一行需要判断的节点都可以用类似于二分的方式来实现,可以将这棵二叉树看作是一棵完全二叉树,那么当要为下一行设置每一个节点的情况时,我们考虑采用二分当前节点的下一节点即可。代码如下:

public class printTree{    public int getDepth(TreeNode root)   //求一棵二叉树的树高    {        if(root == null)            return 0;        int left = getDepth(root.left);        int right = getDepth(root.right);        return 1 + Math.max(left,right);    }    public List<List<String>> printTree(TreeNode root)    {        int depth = getDepth(root);        List<List<String>> list = new ArrayList<>();        int n = 1;        for(int i = 0; i < depth; i++)            n = n << 1;    //首先来求一棵树高为depth的二叉树的所有节点个数,答案是2^depth - 1        n = n - 1;         //这里需要减个1        for(int i = 0; i < depth; i++)        {            List<String> li = new ArrayList<String>(n);            for(int j = 0; j < n; j++)                li.add("");            list.add(li);        }                  //然后是将每一行的list先设置为空节点        subPrint(list,root,0,n,1,depth);        return list;    }    public List<List<String>> subPrint(List<List<String>> list,TreeNode root,int left,int right,int cur,int max)    {        if(cur > max || root == null || left > right)     //设置递归结束的条件,为当前行号大于树高 或 当前节点为空 或 左下标大于了右下标            return null;        int mid = (left + right) / 2;                         //二分查找中间节点        list.get(cur-1).set(mid,String.valueOf(root.val));    //找到相应的中间节点后,然后将在二维的list中设置        subPrint(list,root.left,left,mid - 1,cur + 1,max);    //对左子树的中间节点进行递归操作        subPrint(list,root.right,mid + 1,right,cur + 1,max);  //对右子树的中间节点进行递归操作,主要对上下界限进行控制        return list;         }}
做了这道题,还是很有感触,一开始并没有想到每一层与上一层之间的关系,其实就是简单的二分查找中间节点的关系,所以可以利用二分查找的递归方法来求解,就能非常快地得到结果。希望能多做题目,多总结,多思考,所记忆这一类题目的共性。



原创粉丝点击