此题是关于二叉树的,先回顾一下关于二叉树的相关知识
二叉树的先序遍历(根节点,左孩子,右孩子):
public class PreTreeSolution {
public static String preorderTraversal(TreeNode root) {
StringBuilder builder = new StringBuilder();
//用栈来存储节点
Stack<TreeNode> stack = new Stack<TreeNode>();
//根节点入栈
stack.push(root);
while(!stack.empty()){
//栈顶元素出栈
TreeNode n = stack.pop();
builder.append(n.val);
//由于是 先序,所以让右节点先入栈
if(n.right != null){
stack.push(n.right);
}
//左节点再入栈,这样出栈的时候左节点就可以先出栈
if(n.left != null){
stack.push(n.left);
}
}
return builder.toString();
}
public static void main(String[] args) {
TreeNode rootNode = new TreeNode(1);
TreeNode node1 = new TreeNode(2);
TreeNode node2 = new TreeNode(3);
TreeNode node3 = new TreeNode(4);
TreeNode node4 = new TreeNode(5);
rootNode.left = node1;
rootNode.right = node2;
node1.left = node3;
node1.right = node4;
String resultString = preorderTraversal(rootNode);
System.out.println(resultString);
}
}
class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x) {
val = x;
}
}
public static String inorderTraversal(TreeNode root) {
StringBuilder builder = new StringBuilder();
if(root == null){
return null;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p =root;
while(!stack.isEmpty() || p!=null){
if(p!=null){
stack.push(p);
p = p.left;
}
else{
TreeNode t = stack.pop();
builder.append(t.val);
p = t.right;
}
}
return builder.toString();
}
二叉树的后序遍历(左孩子,右孩子,根节点):
public static String postOrderTraversal(TreeNode root) {
StringBuilder builder = new StringBuilder();
if(root == null){
return null;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
//根节点入栈
stack.push(root);
while(!stack.isEmpty()){
//peek method : Looks at the object at the top of this stack without removing it from the stack.
TreeNode temp = stack.peek();
//如果节点左右孩子都为空,则出栈输出值
if(temp.left == null && temp.right == null){
TreeNode pop = stack.pop();
builder.append(pop.val);
}
else{
//如果右孩子不为空,则入栈
if(temp.right != null){
stack.push(temp.right);
//右孩子入栈后置为空。因为节点输出值时判断该节点的左右孩子是否为空,
//所以如果该节点的右孩子入栈后不置为空,则该节点出栈输出值时判断就会出现问题。
temp.right = null;
}
//如果左孩子不为空,则入栈,入栈后左孩子置为空,原因如上
if(temp.left != null){
stack.push(temp.left);
temp.left = null;
}
}
}
return builder.toString();
}
更详细的内容请戳 参考链接
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
则本道题对二叉树的遍历属于先序遍历。先看代码,本代码耗时24毫秒,beats 77.8%:
public static String tree2str(TreeNode t) {
StringBuilder builder = new StringBuilder();
if(t == null){
return null;
}
Stack<Object> stack = new Stack<Object>();
//根节点入栈
stack.push(t);
while(!stack.empty()){
//栈中包含TreeNode类型的二叉树节点,也包含String类型的括号
Object temp = stack.pop();
//判断是否为TreeNode类型
if(temp instanceof TreeNode){
TreeNode node = (TreeNode) temp;
builder.append(node.val);
//如果该节点左右节点都为空,则不执行任何代码,否则,执行以下代码
if(node.left != null || node.right != null){
if(node.right != null){
stack.push(")");
stack.push(node.right);
stack.push("(");
}
if(node.left != null){
stack.push(")");
stack.push(node.left);
stack.push("(");
}else{
stack.push(")");
stack.push("(");
}
}
}
//判断是否为String类型
if(temp instanceof String){
//String类型直接拼接到builder
builder.append((String) temp);
}
}
return builder.toString();
}