zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java-Leetcode- Binary Tree Traversal

JAVALeetCode Tree Binary Traversal
2023-09-27 14:26:36 时间
public List Integer inorderTraversal(TreeNode root) {

 List Integer result = new ArrayList Integer ();

 if(root==null)

 return result;

 Stack TreeNode stack = new Stack TreeNode ();

 stack.push(root);

 while(!stack.isEmpty()){

 TreeNode top = stack.peek();

 if(top.left!=null){

 stack.push(top.left);

 top.left=null;

 }else{

 result.add(top.val);

 stack.pop();

 if(top.right!=null){

 stack.push(top.right);

 return result;

}

【LeetCode】Verify Preorder Serialization of a Binary Tree(331) One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node s value. If it is a null node, we record using a sentinel value such as #.
二分法猜数字的游戏应该每个人都知道,通过对猜测数字“大了”、“小了”的情况判断,来猜出最终的数字。序列范围为 的集合,复杂度为 ,即最多需要 次可以猜到最终数字。