|
1 | 1 | package datastructure.tree;
|
2 | 2 |
|
| 3 | +import common.Node; |
3 | 4 | import common.TreeNode;
|
4 | 5 |
|
5 | 6 | import java.util.ArrayList;
|
@@ -86,5 +87,81 @@ public TreeNode buildTree(int[] inorder, int[] postorder) {
|
86 | 87 | root.right=buildTree(inorderRight,postorderRight);
|
87 | 88 | return root;
|
88 | 89 | }
|
| 90 | + //从前序与中序遍历序列构造二叉树 |
| 91 | + public TreeNode buildTree2(int[] preorder, int[] inorder) { |
| 92 | + if(preorder == null || inorder == null || preorder.length==0){ |
| 93 | + return null; |
| 94 | + } |
| 95 | + return build(preorder,0,preorder.length-1,inorder,0,inorder.length-1); |
| 96 | + } |
| 97 | + public TreeNode build(int[] preorder, int preLeft,int preRight,int[] inorder,int inLeft,int inRight){ |
| 98 | + if (inLeft > inRight || preLeft >preRight) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + int rootValue=preorder[preLeft]; |
| 102 | + TreeNode root=new TreeNode(rootValue); |
| 103 | + if(preLeft==preRight){ |
| 104 | + return root; |
| 105 | + } |
| 106 | + int rootIndex=-1; |
| 107 | + for(int i=inLeft;i<=inRight;i++){ |
| 108 | + if(inorder[i]==rootValue){ |
| 109 | + rootIndex=i; |
| 110 | + } |
| 111 | + } |
| 112 | + //左子树的长度 |
| 113 | + int leftLength=rootIndex-inLeft; |
| 114 | + //前序序列中左子树的最后一个节点 |
| 115 | + int leftPreEnd = preLeft + leftLength; |
| 116 | + root.left=build(preorder,preLeft+1,leftPreEnd,inorder,inLeft,rootIndex-1); |
| 117 | + root.right=build(preorder,leftPreEnd+1,preRight,inorder,rootIndex+1,inRight); |
| 118 | + return root; |
| 119 | + } |
| 120 | + //填充每个节点的下一个右侧节点指针 |
| 121 | + //给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点 |
| 122 | + public Node connect(Node root) { |
| 123 | + if(root==null){ |
| 124 | + return null; |
| 125 | + } |
| 126 | + if(root.left!=null){ |
| 127 | + //完美二叉树,所以有左孩子必定有右孩子 |
| 128 | + root.left.next=root.right; |
| 129 | + if(root.next!=null){ |
| 130 | + root.right.next=root.next.left; |
| 131 | + } |
| 132 | + } |
| 133 | + connect(root.left); |
| 134 | + connect(root.right); |
| 135 | + return root; |
| 136 | + } |
| 137 | + //填充每个节点的下一个右侧节点指针 II |
| 138 | + //填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。 |
| 139 | + public Node connect2(Node root) { |
| 140 | + if(root==null){ |
| 141 | + return null; |
| 142 | + } |
| 143 | + Node rootNextFirstChildNode=null; |
| 144 | + Node p=root.next; |
| 145 | + //找到root的兄弟节点中从左往右第一个不为空的孩子节点。 |
| 146 | + while(p!=null){ |
| 147 | + if(p.left!=null){ |
| 148 | + rootNextFirstChildNode=p.left; |
| 149 | + break; |
| 150 | + }else if(p.right!=null){ |
| 151 | + rootNextFirstChildNode=p.right; |
| 152 | + break; |
| 153 | + } |
| 154 | + p=p.next; |
| 155 | + } |
| 156 | + if(root.left!=null){ |
| 157 | + root.left.next=root.right==null?rootNextFirstChildNode:root.right; |
| 158 | + } |
| 159 | + if(root.right!=null){ |
| 160 | + root.right.next=rootNextFirstChildNode; |
| 161 | + } |
| 162 | + connect2(root.right); |
| 163 | + connect2(root.left); |
| 164 | + return root; |
| 165 | + } |
89 | 166 | }
|
90 | 167 |
|
0 commit comments