Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>二叉树前序/中序/后序遍历</h1>
<p>使用递归/非递归</p>
<div id="res"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                class Node {
	constructor(data, left, right) {
		this.data = data;
		this.left = left;
		this.right = right;
	}
	getData() {
		return this.data;
	}
}

class BST {
	constructor() {
		this.root = null;
	}
	insert(data) {
		var newNode = new Node(data, null, null);
		if (!this.root) {
			this.root = newNode;
			return;
		}
		var curNode = this.root;
		var parent;
		while (true) {
			parent = curNode;
			if (data < parent.data) {
				curNode = curNode.left;
				if (!curNode) {
					parent.left = newNode;
					break;
				}
			} else {
				curNode = curNode.right;
				if (!curNode) {
					parent.right = newNode;
					break;
				}
			}
		}
	}
}

function bstTraverse() {
	var res = [];
	
	var clearRes = function(){
		res = [];
	}
	var dlrOrder = function(node) {
		if (node) {
			res.push(node.getData()); // 前序遍历
			dlrOrder(node.left);
			dlrOrder(node.right);
		}
	};
	var dlrOrderNoR = function(node) {
		var queue = []; // 前序遍历(非递归)
		var _node = node;
		while (_node || queue.length > 0) {
			res.push(_node.getData());
			queue.unshift(_node);
			_node = _node.left;
			while (!_node && queue.length > 0) {
				_node = queue.shift();
				_node = _node.right;
			}
		}
	};
	var ldrOrder = function(node) {
		if (node) {
			ldrOrder(node.left); // 中序遍历,从小到大
			res.push(node.getData());
			ldrOrder(node.right);
		}
	};
	var ldrOrderNoR = function(node) {
		var queue = []; // // 中序遍历(非递归),从小到大
		var _node = node;
		while (_node || queue.length > 0) {
			if (_node.left) {
				queue.unshift(_node);
				_node = _node.left;
			} else {
				res.push(_node.getData());
				_node = _node.right;
				while (!_node && queue.length > 0) {
					_node = queue.shift();
					res.push(_node.getData());
					_node = _node.right;
				}
			}
		}
	};
	var lrdOrder = function(node) {
		if (node) {
			lrdOrder(node.left); // 后序遍历
			lrdOrder(node.right);
			res.push(node.getData());
		}
	};
	var levelOrder = function(node) {
		var queue = []; // 层序遍历
		var _node = node;
		while (_node) {
			res.push(_node.getData());
			if (_node.left) {
				queue.push(_node.left);
			}
			if (_node.right) {
				queue.push(_node.right);
			}
			_node = queue.shift();
		}
	};
	var getRes = function() {
		return res;
	};
	return {
		dlrOrder: dlrOrder,
		dlrOrderNoR: dlrOrderNoR,
		ldrOrder: ldrOrder,
		ldrOrderNoR: ldrOrderNoR,
		lrdOrder: lrdOrder,
		levelOrder: levelOrder,
		getRes: getRes,
		clearRes: clearRes
	};
}

var bst = new BST();
bst.insert(23);
bst.insert(22);
bst.insert(30);
bst.insert(33);
bst.insert(16);
bst.insert(45);
var traverse = bstTraverse();
traverse.ldrOrderNoR(bst.root);
console.log(traverse.getRes());
document.querySelector("#res").innerHTML += "<b>中序遍历结果: </b>"+traverse.getRes() + '</br>';
traverse.clearRes();
traverse.dlrOrderNoR(bst.root);
console.log(traverse.getRes());
document.querySelector("#res").innerHTML += "<b>前序遍历结果: </b>"+traverse.getRes() + '</br>';
traverse.clearRes();
traverse.lrdOrder(bst.root);
console.log(traverse.getRes());
document.querySelector("#res").innerHTML += "<b>后序遍历结果: </b>"+traverse.getRes() + '</br>';
traverse.clearRes();
traverse.levelOrder(bst.root);
console.log(traverse.getRes());
document.querySelector("#res").innerHTML += "<b>层序遍历结果: </b>"+traverse.getRes() + '</br>';
              
            
!
999px

Console