转换有序链表来平衡二叉树没有返回正确的?链表、正确、二叉树

2023-09-11 06:52:55 作者:三分玩笑╮七分真

下面是我写的,用于转换的LinkedList 平衡二分查找树键的方法。我得到 BST ,但不均衡。为什么会这样?

 公共静态节点headNode;

公共静态IntTreeNode convertLinkedListToBST(节点node){
            INT LEN = getCount将(节点);
            headNode =节点;
            返回convertLinkedListToBSThelper(节点0,LEN-1);


    }
    //http://www.programcreek.com/2013/01/leet$c$c-convert-sorted-list-to-binary-search-tree-java/
    公共静态IntTreeNode convertLinkedListToBSThelper(节点的节点,诠释开始,诠释完){
        如果(开始>结束)
            返回null;
        INT =中旬开始+端>> 1;
        IntTreeNode左= convertLinkedListToBSThelper(节点开始,中-1);
        IntTreeNode根=新IntTreeNode(headNode.data);
        headNode = headNode.next;
        IntTreeNode右= convertLinkedListToBSThelper(节点,中+ 1,结束);
        root.left =左;
        root.right =权利;
        返回根;
    }

私有静态诠释getCount将(节点node){
    诠释计数= 0;
    节点电流=节点;
    而(电流!= NULL){
        电流= current.next;
        算上++;
        }
    返回计数;
}
 

下面是主要的方法:

 节点node =新节点(1);
node.next =新节点(2);
node.next.next =新节点(3);
node.next.next.next =新节点(4);
node.next.next.next.next =新节点(5);
node.next.next.next.next.next =新节点(6);
node.next.next.next.next.next.next =新节点(7);
node.next.next.next.next.next.next.next =新节点(8);

的System.out.println(***********);
IntTreeNode RESULT1 = convertLinkedListToBST(节点);
的System.out.println(preOrder);
打印preOrder(RESULT1);
的System.out.println(序);
printInOrder(RESULT1);
的System.out.println(后序);
printPostOrder(RESULT1);
的System.out.println();
的System.out.println(isValidBST(RESULT1));
名单<整数GT;名单=新的ArrayList<>();
printLevelorder(RESULT1,清单);
的System.out.println(名单);
 
平衡二叉树转为排序的双向链表 知道孩子如何求双亲

下面是我得到(格式为便于阅读)的输出:

  preOrder 4,1,2,3,5,6,7,8,
序1,2,3,4,5,6,7,8,
后序1,2,3,5,6,7,8,4,
真正
[4,2,6,1,3,5,7,8]
 

等级秩序和preOrder不匹配,建立一个独特的树。 在这里任何提示?

解决方案

您convertLinkedListToBSThelper和放大器; getCount将方法工作well.I已经用我的code的两种方法。

进口的java.util。*; 一流的解决方案{ 静态节点headNode;     公共静态无效的主要(字符串的args []){          节点node =新节点(1);     headNode =节点;     节点节点1 =新节点(2);     节点节点2 =新节点(3);     节点节点3 =新节点(4);     节点节点4 =新节点(5);     节点Node5的=新节点(6);     节点node6 =新节点(7);     节点node7 =新节点(8);     node.setnext(节点);     node1.setnext(节点2);     node2.setnext(节点3);     node3.setnext(节点4);     node4.setnext(Node5的);     node5.setnext(node6);     node6.setnext(node7);     node7.setnext(空);     INT LEN = getCount将(节点);     节点RESULT1 = convertLinkedListToBSThelper(节点,0,LEN-1); 的System.out.println(preOrder); preorder(RESULT1); 的System.out.println(); 的System.out.println(序); 序(RESULT1); 的System.out.println(); 的System.out.println(后序); 后序(RESULT1); 的System.out.println(levelOrder); levelorder(RESULT1); } 公共静态节点convertLinkedListToBSThelper(节点的节点,诠释开始,诠释完){         如果(开始>结束)             返回null;         INT =中旬开始+端>> 1;         节点离开= convertLinkedListToBSThelper(节点开始,中-1);         节点根=新节点(headNode.getdata());                 headNode = headNode.next;         节点右= convertLinkedListToBSThelper(节点上,中旬+ 1,结束);         root.setleft(左);         root.setright(右);         返回根;     } 私有静态诠释getCount将(节点node){     诠释计数= 0;     节点电流=节点;     而(电流!= NULL){         电流= current.next;         算上++;         }     返回计数; } 公共静态无效preorder(节点温度){   如果(临时== NULL)      返回;    System.out.print(temp.data +);    preorder(temp.getleft());    preorder(temp.getright()); } 公共静态无效序(节点温度){   如果(临时== NULL)      返回;    序(temp.getleft());    System.out.print(temp.data +);    序(temp.getright()); } 公共静态无效后序(节点温度){ 如果(临时== NULL) 返回; 后序(temp.getleft());        后序(temp.getright()); System.out.print(temp.data +); } 公共静态无效levelorder(节点温度) {    队列<节点> Q =新的LinkedList<节点>();    q.add(临时);    而(!q.isEmpty()){ 节点= q.remove(); System.out.print(a.getdata()+); 如果(a.getleft()!= NULL) q.add(a.getleft()); 如果(a.getright()!= NULL) q.add(a.getright());    }     }      } 类节点{     int数据;     下一节点;     节点左侧;     节点权利;     公共节点(int i)以{ this.data =我;     // TODO自动生成构造函数存根 }     公共无效setnext(节点温度){         this.next =温度;     }     公共节点GETNEXT(){         返回this.next;     }     公共无效setleft(节点温度){         this.left =温度;     }     公共节点getleft(){         返回this.left;     }     公共无效setright(节点温度){         this.right =温度;     }     公共节点GetRight时(){         返回this.right;     }     公众诠释的getData(){       返回this.data;     } }

我认为你正在做的错误在一些遍历方法。 请检查出来有mine.Still,如果你有问题,只贴上整个code。 顺便说一句我的code给出输出

  preOrder
4 2 1 3 6 5 7 8
为了
1 2 3 4 5 6 7 8
后序
1 3 2 5 8 7 6 4
levelOrder
4 2 6 1 3 5 7 8
 

一件事总有唯一的二进制树(可能是平衡的BST或BST)给定下列组合

 中序和preorder。
序和后序。
序和Level-秩序。
 

Here are key methods I wrote for converting linkedList to Balanced BinarySearch Tree. I get BST but it is not balanced. why is it so?

public static Node headNode;

public static IntTreeNode convertLinkedListToBST(Node node){
            int len = getCount(node);
            headNode = node;
            return convertLinkedListToBSThelper(node, 0, len-1);


    }
    //http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/
    public static IntTreeNode convertLinkedListToBSThelper(Node node, int start, int end) {
        if (start>end)
            return null;
        int mid=start+end >>1 ;
        IntTreeNode left = convertLinkedListToBSThelper(node, start, mid-1);
        IntTreeNode root = new IntTreeNode(headNode.data);
        headNode=headNode.next;
        IntTreeNode right = convertLinkedListToBSThelper(node, mid+1, end);
        root.left=left;
        root.right=right;
        return root;
    }

private static int getCount(Node node){
    int count=0;
    Node current = node;
    while(current!=null){
        current=current.next;
        count++;
        }
    return count;
}

Here is main method:

Node node = new Node(1);
node.next=new Node(2);
node.next.next=new Node(3);
node.next.next.next=new Node(4);
node.next.next.next.next=new Node(5);
node.next.next.next.next.next=new Node(6);
node.next.next.next.next.next.next=new Node(7);
node.next.next.next.next.next.next.next=new Node(8);

System.out.println("***********");
IntTreeNode result1 = convertLinkedListToBST(node);
System.out.println("preOrder");
printPreOrder(result1);
System.out.println("inOrder");
printInOrder(result1);
System.out.println("postOrder");
printPostOrder(result1);
System.out.println();
System.out.println(isValidBST(result1));
List<Integer> list = new ArrayList<>();
printLevelorder(result1, list);
System.out.println(list);

Here is the output I get (formatted for readability):

preOrder  4, 1, 2, 3, 5, 6, 7, 8, 
inOrder   1, 2, 3, 4, 5, 6, 7, 8, 
postOrder 1, 2, 3, 5, 6, 7, 8, 4, 
true
[4, 2, 6, 1, 3, 5, 7, 8]

Level order and preOrder does not match to build a unique tree. Any tips here ?

解决方案

Your convertLinkedListToBSThelper & getcount method work well.I have used your both method in my code.

import java.util.*;


class Solution{
static Node headNode;
    public static void main(String args[]){
    
    Node node = new Node(1);
    headNode=node;
    Node node1 = new Node(2);
    Node node2 = new Node(3);
    Node node3 = new Node(4);
    Node node4 = new Node(5);
    Node node5 = new Node(6);
    Node node6 = new Node(7);
    Node node7 = new Node(8);
    node.setnext(node1);
    node1.setnext(node2);
    node2.setnext(node3);
    node3.setnext(node4);
    node4.setnext(node5);
    node5.setnext(node6);
    node6.setnext(node7);
    node7.setnext(null);
    int len=getCount(node);
    Node result1=convertLinkedListToBSThelper(node, 0, len-1);

System.out.println("preOrder");
preorder(result1);
System.out.println();
System.out.println("inOrder");
inorder(result1);
System.out.println();
System.out.println("postOrder");
postorder(result1);
System.out.println("levelOrder");
levelorder(result1);
}

public static Node convertLinkedListToBSThelper(Node node, int start, int end) {
        if (start>end)
            return null;
        int mid=start+end >>1 ;
        Node left = convertLinkedListToBSThelper(node, start, mid-1);
        Node root = new Node(headNode.getdata());
       
        headNode=headNode.next;
        Node right = convertLinkedListToBSThelper(node, mid+1, end);
        root.setleft(left);
        root.setright(right);
        return root;
    }

private static int getCount(Node node){
    int count=0;
    Node current = node;
    while(current!=null){
        current=current.next;
        count++;
        }
    return count;
}

public static void preorder(Node temp){
  if(temp==null)
     return;
   System.out.print(temp.data+" ");
   preorder(temp.getleft());
   preorder(temp.getright());
}

public static void inorder(Node temp){
  if(temp==null)
     return;
   inorder(temp.getleft());
   System.out.print(temp.data+" ");
   inorder(temp.getright());
}

public static void postorder(Node temp){
	  if(temp==null)
	     return;
	   postorder(temp.getleft());
       postorder(temp.getright());
	   System.out.print(temp.data+" ");	
}

public static void levelorder(Node temp)
{
   Queue<Node> q=new LinkedList<Node>();
   q.add(temp);
   while(!q.isEmpty()){
	   Node a=q.remove();
	   System.out.print(a.getdata()+" ");
	   if(a.getleft()!=null)
	   q.add(a.getleft());
	   if(a.getright()!=null)
	   q.add(a.getright());
   }
   
}
    
} 
class Node{
    int data;
    Node next;
    Node left;
    Node right;
    public Node(int i) {
		this.data=i;
    	// TODO Auto-generated constructor stub
	}
	
    public  void  setnext(Node temp){
        this.next=temp;
    }
    public  Node getnext(){
        return this.next;
    }
    public  void  setleft(Node temp){
        this.left=temp;
    }
    public  Node getleft(){
        return this.left;
    }
    public  void  setright(Node temp){
        this.right=temp;
    }
    public  Node getright(){
        return this.right;
    }
    public int getdata(){
      return this.data;
    }

}

I think you are doing mistake in some traversal method. Please check it out with mine.Still if you have problem just paste whole code. btw my code gives output

preOrder
4 2 1 3 6 5 7 8 
inOrder
1 2 3 4 5 6 7 8 
postOrder
1 3 2 5 8 7 6 4 
levelOrder
4 2 6 1 3 5 7 8 

One more thing there is always unique binary tree(may be balance BST or BST) with given following combination

Inorder and Preorder.
Inorder and Postorder.
Inorder and Level-order.