java寬度優先將二叉樹存成數組

來源:互聯網
上載者:User

標籤:++   演算法   style   maxwidth   void   super   strong   sem   文章   

題目:

有一棵二叉樹,請設計一個演算法,按照層次列印這棵二叉樹。給定二叉樹的根結點root,

請返回列印結果,結果按照每一層一個數組進行儲存,所有數組的順序按照層數從上往下,且每一層的數組內元素按照從左往右排列。保證結點數小於等於500。

解題思路:

類似上一篇文章中的思路

源碼:

package ss.entity;public class TreeNode {    int val;    TreeNode left;    TreeNode right;        public int getVal() {        return val;    }    public void setVal(int val) {        this.val = val;    }    public TreeNode getLeft() {        return left;    }    public void setLeft(TreeNode left) {        this.left = left;    }    public TreeNode getRight() {        return right;    }    public void setRight(TreeNode right) {        this.right = right;    }    public TreeNode(int x) { val = x; }        public boolean equals(Object obj) {        return super.equals(obj);    }}
package ss.tree;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Queue;import ss.entity.TreeNode;/** * 有一棵二叉樹,請設計一個演算法,按照層次列印這棵二叉樹。 * 給定二叉樹的根結點root, * 請返回列印結果, * 結果按照每一層一個數組進行儲存, * 所有數組的順序按照層數從上往下, * 且每一層的數組內元素按照從左往右排列。保證結點數小於等於500。 * */public class TreeDemo03 {    public static void main(String[] args){        TreeNode tree = new TreeNode(1);        tree.setLeft(new TreeNode(2));        tree.setRight(new TreeNode(3));                tree.getLeft().setLeft(new TreeNode(4));                tree.getRight().setLeft(new TreeNode(5));        tree.getRight().setRight(new TreeNode(6));                tree.getRight().getLeft().setLeft(new TreeNode(7));        tree.getRight().getLeft().setRight(new TreeNode(8));                int[][] a = TreeDemo03.printTree(tree);        for(int[] b : a){            for(int c : b){                System.out.print(c);            }            System.out.println();        }    }    public static int[][] printTree(TreeNode root) {        TreeNode last = root;        TreeNode nlast = null;        Queue queue = new LinkedList();        queue.add(root);                List<List> list = new ArrayList<List>();        List<Integer> temp = new ArrayList<Integer>();         int maxWidth = 0;         while(!queue.isEmpty()){            TreeNode tempNode = (TreeNode)queue.poll();            temp.add(tempNode.getVal());            if(null != tempNode.getLeft()){                queue.add(tempNode.getLeft());                nlast = tempNode.getLeft();            }            if(null != tempNode.getRight()){                queue.add(tempNode.getRight());                nlast = tempNode.getRight();            }            if(tempNode.equals(last)){                last = nlast;                list.add(temp);                if(temp.size() > maxWidth)                    maxWidth = temp.size();                temp = new ArrayList<Integer>();            }else{                continue;            }        }        int[][] result = new int[list.size()][maxWidth];        for(int i = 0; i < list.size(); i++){            List<Integer> ll = list.get(i);            for(int j = 0; j < list.get(i).size(); j++){                result[i][j] = ll.get(j);            }        }        return result;    }}

 

java寬度優先將二叉樹存成數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.