方法1:地柜
我不知道为啥,lintcode就是报空指针,同一个例子,我跑就不报,他跑就报,实在是有病。
process作用就是返回一个排序后的链表,那么left得到的就是左边排序的链表,right就是得到右边的,如果只有一个节点那么就是这个节点了对吧,和归并排序一个思想(merge,,虽然我有点忘了归并拍虚了)。
Tpublic class Solution {
/**
* @param root: root of a tree
* @return: head node of a doubly linked list
*/
public TreeNode treeToDoublyList(TreeNode root) {
// Write your code here.
return process(root);
}
//返回排序后的双向链表
public TreeNode process(TreeNode root){
if(root==null){
return null;
}
TreeNode left=process(root.left);
TreeNode right=process(root.right);
TreeNode left1=left;
if(left1!=null){
while(left1.right!=null){
left1=left1.right;
}
}
root.left=null;
root.right=null;
if(left1!=null){
left1.right=root;
root.left=left1;
}
if(right!=null){
root.right=right;
right.left=root;
}
return left==null?root:left;
}
}
方法2:
public static void main(String[] args) {
//int[] arr = getRandomArray(10);
TreeNode root=new TreeNode(4);
TreeNode left1=new TreeNode(2);
TreeNode right1=new TreeNode(5);
root.left=left1;
root.right=right1;
TreeNode left2=new TreeNode(1);
TreeNode right2=new TreeNode(3);
left1.left=left2;
left1.right=right2;
TreeNode t=treeToDoublyList(root);
while(t!=null) {
System.out.println(t.val);
t=t.right;
}
}
public static TreeNode treeToDoublyList(TreeNode root) {
// Write your code here.
return process1(root)[0];
}
//返回排序后的双向链表
public static TreeNode process(TreeNode root){
if(root==null){
return null;
}
TreeNode left=process(root.left);
TreeNode right=process(root.right);
TreeNode left1=left;
if(left1!=null){
while(left1.right!=null){
left1=left1.right;
}
}
root.left=null;
root.right=null;
if(left1!=null){
left1.right=root;
root.left=left1;
}
if(right!=null){
root.right=right;
right.left=root;
}
return left1==null?root:left;
}
public static TreeNode[] process1(TreeNode root) {
if(root==null) {
return new TreeNode[2];
}
TreeNode[] left=process1(root.left);
TreeNode[] right=process1(root.right);
root.left=null;
root.right=null;
if(left!=null&&left[1]!=null) {
left[1].right=root;
root.left=left[1];
}
if(right!=null&&right[0]!=null) {
root.right=right[0];
right[0].left=root;
}
TreeNode[] res=new TreeNode[2];
res[0]=left[0]==null?root:left[0];
res[1]=right[1]==null?root:right[1];
//下面这些就是上面的三目运算符,这个不要忘了
// if(left[1]==null) {
// res[0]=root;
// }else {
// res[0]=left[0];
// }
// if(right[1]==null) {
// res[1]=root;
// }else {
// res[1]=right[1];
// }
return res;
}
方法3:中序遍历就好了,然后自己做节点去连
public static Node convert1(Node head) {
Queue<Node> queue = new LinkedList<Node>();
inOrderToQueue(head, queue);
if (queue.isEmpty()) {
return head;
}
head = queue.poll();
Node pre = head;
pre.left = null;
Node cur = null;
while (!queue.isEmpty()) {
cur = queue.poll();
pre.right = cur;
cur.left = pre;
pre = cur;
}
pre.right = null;
return head;
总结:树的遍历,记住,地柜的时候,左右子树返回相同的结构,所以返回的信息是左右信息的整合,而不是单个树的信息
Leetcode,有序数组转平衡二叉树
难度是:easy。,确实很简单,就是个地柜,主要是二分选点,保证平衡这个很重要,只要二分就一定是平衡的,记住他,其次就是basecase,通过简单的画画图,走几遍流程就能知道如果left’<right此时返回空
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
//二分法,每次取中点,然后左边放入左子树,右边柚子树
return process(nums,0,nums.length-1);
}
public TreeNode process(int[] nums,int left,int right){
if(left>right){
return null;
}
int mid=left+(right-left)/2;
TreeNode root=new TreeNode(nums[mid]);
root.left=process(nums,left,mid-1);
root.right=process(nums,mid+1,right);
return root;
}
}
上面的题还有个要点,这个虽然我目前知道,但是保不准那次就忘了,就是数值的越界问题,取中点的时候,一定mid=left+(right-left)/2 如果用left+right,真的很容易就爆int,真的,我遇到过这个问题,引以为戒