Untitled
unknown
plain_text
2 years ago
2.2 kB
3
Indexable
import java.io.*; import java.util.*; import crio.ds.Tree.TreeNode; /* Definition of TreeNode class TreeNode { public long val; public TreeNode left; public TreeNode right; public TreeNode (long x) { val = x; left = null; right = null; } } Use new TreeNode(data) to create new Node */ class Solution { static int ans = 0; public int longestSlideInTreeRec(TreeNode root, int dir) { if(root == null) { return 0; } int l = longestSlideInTreeRec(root.left, -1); int r = longestSlideInTreeRec(root.right, 1); ans = Math.max(ans, Math.max(l+1,r+1)); if(dir == -1) { return l+1; } else { return r+1; } } public int longestSlideInTree(TreeNode root) { longestSlideInTreeRec(root, 0); return ans - 1; } } /* Crio Methodology Milestone 1: Understand the problem clearly 1. Ask questions & clarify the problem statement clearly. 2. Take an example or two to confirm your understanding of the input/output & extend it to test cases Milestone 2: Finalize approach & execution plan 1. Understand what type of problem you are solving. a. Obvious logic, tests ability to convert logic to code b. Figuring out logic c. Knowledge of specific domain or concepts d. Knowledge of specific algorithm e. Mapping real world into abstract concepts/data structures 2. Brainstorm multiple ways to solve the problem and pick one 3. Get to a point where you can explain your approach to a 10 year old 4. Take a stab at the high-level logic & write it down. 5. Try to offload processing to functions & keeping your main code small. Milestone 3: Code by expanding your pseudocode 1. Make sure you name the variables, functions clearly. 2. Avoid constants in your code unless necessary; go for generic functions, you can use examples for your thinking though. 3. Use libraries as much as possible Milestone 4: Prove to the interviewer that your code works with unit tests 1. Make sure you check boundary conditions 2. Time & storage complexity 3. Suggest optimizations if applicable */
Editor is loading...