Binary Trees
unknown
java
14 days ago
1.9 kB
39
Indexable
Never
// leetcode 965 ================================= class Solution { public boolean checkIfUnivalued(TreeNode root, int OGRootValue){ if(root == null){ return true; } if(root.val != OGRootValue){ return false; } boolean isLeftUnivalued = checkIfUnivalued(root.left, OGRootValue); boolean isRightUnivalued = checkIfUnivalued(root.right, OGRootValue); return isRightUnivalued && isLeftUnivalued; } public boolean isUnivalTree(TreeNode root) { int rootValue = root.val; return checkIfUnivalued(root, rootValue); } } // leetcode 226 ========================================================= class Solution { public TreeNode invertTree(TreeNode root) { if(root == null){ return root; } // inverting current position TreeNode temp = root.left; root.left = root.right; root.right = temp; invertTree(root.left); invertTree(root.right); return root; } } // Solution 2 ================================================= class Solution { public TreeNode invertTree(TreeNode root) { if(root == null){ return root; } // inverting current position TreeNode temp = root.right; root.right = invertTree(root.left); root.left = invertTree(temp); return root; } } // leetcode 1325 ================================================== class Solution { public TreeNode removeLeafNodes(TreeNode root, int target) { if(root == null){ return root; } root.left = removeLeafNodes(root.left, target); root.right = removeLeafNodes(root.right, target); if(root.left == null && root.right == null && root.val == target){ return null; } return root; } }
Leave a Comment