Algorithms and Data Structures/Leetcode (3) 썸네일형 리스트형 Leetcode 700. Search in a Binary Search Tree 자바 재귀 class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root == null || val == root.val) return root; return val Optional[TreeNode]: while (root is not None and val != root.val): root = root.left if val < root.val els.. Leetcode - 104. Maximum Depth of Binary Tree - Java DFS if (root == null) return 0; int left = maxDepth(root.left); int right = maxDepth(root.right); // This is the part that counts the number of nodes. return 1 + Math.max(left, right); BFS same as the level order traversal. You just need to return the size or length of the list at the end. List levels = new ArrayList(); if (root == null) { return 0; } Queue queue = new LinkedList(); queue.add(ro.. Leetcode 102 - Binary Tree Level Order Traversal BFS class Solution: def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]: res = [] if root is None: return res q = collections.deque() q.append(root) # use queue data structure since this is a BFS - it goes from left to right. while q: qLen = len(q) # to ensure we're looping through one level at a time. # qLen represents the length of the nodes in each level. level = [] # current le.. 이전 1 다음