您的位置:首页 > 新闻 > 热点要闻 > 无锡点个赞建站_深圳网页设计公司排行_网站宣传推广文案_企业营销型网站建设

无锡点个赞建站_深圳网页设计公司排行_网站宣传推广文案_企业营销型网站建设

2025/3/9 9:04:32 来源:https://blog.csdn.net/blamep/article/details/143109548  浏览:    关键词:无锡点个赞建站_深圳网页设计公司排行_网站宣传推广文案_企业营销型网站建设
无锡点个赞建站_深圳网页设计公司排行_网站宣传推广文案_企业营销型网站建设

1.根据二叉树创建字符串

题目链接:. - 力扣(LeetCode)

class Solution {public String tree2str(TreeNode root) {if(root==null){return null;}StringBuilder stringBuilder=new StringBuilder();tree2strChild(root,stringBuilder);return stringBuilder.toString();}public void tree2strChild(TreeNode t,StringBuilder stringBuilder){if(t==null){return;}stringBuilder.append(t.val);if(t.left!=null){stringBuilder.append("(");tree2strChild(t.left,stringBuilder);stringBuilder.append(")");}else{if(t.right==null){return;}else{stringBuilder.append("()");}}if(t.right!=null){stringBuilder.append("(");tree2strChild(t.right,stringBuilder);stringBuilder.append(")");}else{return;}}
}

2.二叉树前序非递归遍历实现

题目链接:. - 力扣(LeetCode)

public void preOrderTree(TreeNode root){Stack<TreeNode> stack=new Stack<>();if(root==null){return ;}TreeNode cur=root;while (cur!=null || !stack.isEmpty()){while (cur!=null){stack.push(cur);System.out.println(cur.val+" ");cur=cur.left;}TreeNode top=stack.pop();cur=cur.right;}}

3.二叉树中序非递归遍历实现

题目链接:. - 力扣(LeetCode)

public void inOrderTree(TreeNode root){Stack<TreeNode> stack=new Stack<>();if(root==null){return ;}TreeNode cur=root;while (cur!=null || !stack.isEmpty()){while (cur!=null){stack.push(cur);cur=cur.left;}TreeNode top=stack.pop();System.out.println(top.val+" ");cur=cur.right;}}

4.二叉树后序非递归遍历实现

题目链接:. - 力扣(LeetCode)

public void postOrderTree(TreeNode root){Stack<TreeNode> stack=new Stack<>();if(root==null){return ;}TreeNode cur=root;TreeNode prev=null;while (cur!=null || !stack.isEmpty()){while (cur!=null){stack.push(cur);cur=cur.left;}TreeNode top=stack.peek();if(top.right==null || top.right==prev){System.out.println(top.val+" ");stack.pop();prev=top;}else {top=top.right;}}}

希望能对大家有所帮助!!!!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com