剑指Offer二叉搜索树与双向链表

class BinaryTreeNode {
	int value;
	BinaryTreeNode pLeft;
	BinaryTreeNode pRight;
	BinaryTreeNode pNext;
}
class BinaryTree {
	public BinaryTreeNode CreateBinaryTreeNode(int value) {
		BinaryTreeNode pNode = new BinaryTreeNode();
		pNode.value = value;
		pNode.pLeft = null;
		pNode.pRight = null;
		return pNode;
	}

	public void ConnectTreeNodes(BinaryTreeNode pParent, BinaryTreeNode pLeft,
			BinaryTreeNode pRight) {
		if (pParent != null) {
			pParent.pLeft = pLeft;
			pParent.pRight = pRight;
		}
	}
}

public class Convert_BinaryTree {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test1();

//		StringBuffer sb = new StringBuffer("Hello ");
//		StringBuffer str = new StringBuffer("Hi");
//		System.out.println("Before change, sb = " + sb);
//		System.out.println("Before change, str = " + str);
//		changeData(str, sb);
//		System.out.println("After changeData(n), sb = " + sb);
//		System.out.println("After change, str = " + str);
	}

	public static void changeData(StringBuffer str, StringBuffer strBuf) {
		str = strBuf ;
		str.append("World!");
	}

	public static void Test1() {
		BinaryTree bt = new BinaryTree();
		BinaryTreeNode pNode1 = bt.CreateBinaryTreeNode(6);
		BinaryTreeNode pNode2 = bt.CreateBinaryTreeNode(4);
		BinaryTreeNode pNode3 = bt.CreateBinaryTreeNode(8);
		BinaryTreeNode pNode4 = bt.CreateBinaryTreeNode(3);
		BinaryTreeNode pNode5 = bt.CreateBinaryTreeNode(5);
		BinaryTreeNode pNode6 = bt.CreateBinaryTreeNode(7);
		BinaryTreeNode pNode7 = bt.CreateBinaryTreeNode(9);
		bt.ConnectTreeNodes(pNode1, pNode2, pNode3);
		bt.ConnectTreeNodes(pNode2, pNode4, pNode5);
		bt.ConnectTreeNodes(pNode3, pNode6, pNode7);
		System.out.println(Convert(pNode1).value);
	}

	public static BinaryTreeNode Convert(BinaryTreeNode pRoot) {
		BinaryTreeNode pLastNode = null;
		pLastNode = ConvertNode(pRoot, pLastNode);
		BinaryTreeNode pHeadNode = pLastNode;
		while (pHeadNode != null && pHeadNode.pLeft != null) {
			pHeadNode = pHeadNode.pLeft;
			System.out.println(pHeadNode.value);
		}
		return pHeadNode;
	}

	// pLastNode已排好的双向链表中的最后一个结点
	public static BinaryTreeNode ConvertNode(BinaryTreeNode pNode,
			BinaryTreeNode pLastNode) {
		if (pNode == null) {
			return pLastNode;
		}
		BinaryTreeNode pCurrent = pNode;
		if (pCurrent.pLeft != null) {
			pLastNode = ConvertNode(pCurrent.pLeft, pLastNode);
		}
		pCurrent.pLeft = pLastNode;
		if (pLastNode != null) {
			pLastNode.pRight = pCurrent;
		}
		pLastNode = pCurrent;
		if (pCurrent.pRight != null) {
			pLastNode = ConvertNode(pCurrent.pRight, pLastNode);
		}
		return pLastNode;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值